from PyQt5.QtWidgets import *
from PyQt5 import QtGui
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import time
import pymysql
import sys
import os
import sys
import os
os.path.realpath(__file__)
os.path.dirname(os.path.realpath(__file__))
GRANDFA = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
GRANDFA = os.path.dirname(GRANDFA)
#print(os.path.join(GRANDFA,'admin'))
sys.path.append(os.path.join(GRANDFA,'database','admin/')) # 将祖父路径加入sys中
#sys.path.append('/Users/tanxin/PycharmProjects/DB8/database/admin')
import interface.admin.tea_related as tea_related
import database.admin.setting as setting
import interface.admin.admin_main0_Window as admin_main0_Window
import interface.admin.teaadd_Window as teaadd_Window
class tea_related_window(QMainWindow,tea_related.Ui_Form):###点击用户身份弹出的登陆小弹窗
    def __init__(self):
        QMainWindow.__init__(self)
        tea_related.Ui_Form.__init__(self)
        self.setupUi(self)
        self.setWindowTitle("教师信息操作")
        self.table.setColumnCount(6)
        self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.table.setHorizontalHeaderLabels(['学号','姓名','性别','学院','职称','选择'])
        self.sex_box.addItem('ALL')
        self.sex_box.addItem('男')
        self.sex_box.addItem('女')
        self.job_box.addItem('ALL')
        self.job_box.addItem('讲师')
        self.job_box.addItem('副教授')
        self.job_box.addItem('教授')
        self.job_box.addItem('助理教授')
        self.school_box.addItem('ALL')
        self.school_box.addItem("航空学院")
        self.school_box.addItem("航天学院")
        self.school_box.addItem("航海学院")
        self.school_box.addItem("材料学院")
        self.school_box.addItem("机电学院")
        self.school_box.addItem("动力与能源学院")
        self.school_box.addItem("电信学院")
        self.school_box.addItem("计算机学院")
        self.school_box.addItem("外国语学院")
        self.school_box.addItem("软件学院")
        self.school_box.addItem("管理学院")
        self.table.sortItems(0,Qt.AscendingOrder)
        self.find_tea.clicked.connect(self.find)
        self.add_tea.clicked.connect(self.add)
        self.quit.clicked.connect(QCoreApplication.instance().quit)
        self.back.clicked.connect(self.load_back)
    def load_back(self):
        self.pre_window=admin_main0_Window.admin_main0_window()
        self.pre_window.show()
        self.close()
    def add(self):
        self.add_tea_win=teaadd_Window.teaadd_window()
        self.add_tea_win.show()
    def find(self):
        self.sex_cond=self.sex_box.currentText()
        self.job_cond=self.job_box.currentText()
        self.school_cond=self.school_box.currentText()
        self.connect = pymysql.connect(**setting.config)
        self.cursor=self.connect.cursor()
        if(self.sex_cond=='ALL'):
            self.sex_cond='true'
        else:
            self.sex_cond="sex = '%s'"%self.sex_cond
        if (self.job_cond == 'ALL'):
            self.job_cond='true'
        else:
            self.job_cond = "job= '%s'" % self.job_cond
        if (self.school_cond == 'ALL'):
            self.school_cond='true'
        else:
            self.school_cond = "school= '%s'" % self.school_cond
        sql_="select * from teachers where "+self.sex_cond+" and "+self.job_cond+" and "+self.school_cond+';'
        print(sql_)
        self.cursor.execute(sql_)
        re=self.cursor.fetchall()
        print(re)
        self.display_find_result(re)
        self.cursor.close()
        self.connect.close()
    def display_find_result(self,re):
        self.table.setRowCount(0)
        row=self.table.rowCount()
        #self.table.setRowCount(row+1)
        re_len=len(re)
        for l in range(re_len):
            row = self.table.rowCount()
            self.table.setRowCount(row + 1)
            ck=QCheckBox()
            h=QHBoxLayout()
            h.setAlignment(Qt.AlignCenter)
            h.addWidget(ck)
            w=QWidget()
            w.setLayout(h)
            self.table.setItem(row,0,QTableWidgetItem(re[l][0]))
            self.table.setItem(row, 1, QTableWidgetItem(re[l][1]))
            self.table.setItem(row, 2, QTableWidgetItem(re[l][2]))
            self.table.setItem(row, 3, QTableWidgetItem(re[l][3]))
            self.table.setItem(row, 4, QTableWidgetItem(re[l][4]))
            self.table.setCellWidget(row, 5,w)

    def add_stu(self):
        pass
if __name__=='__main__':
    app=QApplication(sys.argv)
    md=tea_related_window()
    md.show()
    sys.exit(app.exec_())